package org.androidpn.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import org.androidpn.R;
import org.androidpn.model.CTalkList;
import org.androidpn.model.CUserInfo;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TalkListAdapter extends BaseAdapter {
	private Context context;
	private List<CTalkList> cTalkLists;
	private CUserInfo cUserInfo;

	public void setcTalkLists(List<CTalkList> cTalkLists) {
		this.cTalkLists = cTalkLists;
	}

	public TalkListAdapter(Context context) {
		this.context = context;
		cUserInfo = AppCache.getInstance().getUseInfo();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cTalkLists.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return cTalkLists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		Viewholder viewholder;
		CTalkList model = cTalkLists.get(position);
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.talk_list_item, null);
			viewholder = new Viewholder();
			viewholder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			viewholder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewholder.ivHead = (ImageView) convertView.findViewById(R.id.iv_head);
			viewholder.tvUnread = (TextView) convertView.findViewById(R.id.tv_unread);
			convertView.setTag(viewholder);
		} else {
			viewholder = (Viewholder) convertView.getTag();
		}
		DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).showImageOnFail(R.drawable.default_icon)
				.showImageForEmptyUri(R.drawable.default_icon).showImageOnLoading(R.drawable.default_icon).build();
		if (cUserInfo != null) {
			if (cUserInfo.getTelephone().contentEquals(model.getMy_telephone())) {
				viewholder.tvName.setText(model.getFriend_name());
				if (!TextUtils.isEmpty(model.getFriend_icon())) {
					String imgurl = ConstanceUtil.HOST_NAME + model.getFriend_icon();
					ImageLoader.getInstance().displayImage(imgurl, viewholder.ivHead, options1);
				}
			} else {
				viewholder.tvName.setText(model.getMy_name());
				if (!TextUtils.isEmpty(model.getMy_icon())) {
					String imgurl = ConstanceUtil.HOST_NAME + model.getMy_icon();
					ImageLoader.getInstance().displayImage(imgurl, viewholder.ivHead, options1);
				}
			}
		}
		SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String sdata = dataFormat.format(model.getTalk_time());
		viewholder.tvTime.setText(sdata);
		if (model.getUnread_count() > 0) {
			viewholder.tvUnread.setText(model.getUnread_count() + "");
			viewholder.tvUnread.setVisibility(View.VISIBLE);
		} else {
			viewholder.tvUnread.setText("");
			viewholder.tvUnread.setVisibility(View.GONE);
		}
		return convertView;
	}

	class Viewholder {
		ImageView ivHead;
		TextView tvName;
		TextView tvTime;
		TextView tvUnread;
	}

}
